/**
 * 
 */
package array.passed;

/**
 * @author xyyi
 *
 */
public class RemoveElement {

	/**
	Given an array and a value, remove all instances of that value in place and return the new length.
	
	The order of elements can be changed. It doesn't matter what you leave beyond the new length.
	 */
	public int removeElement(int[] A, int elem) {
		// Start typing your Java solution below
		// DO NOT write main() function
		if (A == null || A.length == 0)
			return 0;

		int slow = 0, fast = 0;
		while (fast < A.length) {
			if (A[fast] != elem) {
				A[slow++] = A[fast];
			}
			fast++;
		}

		return slow;
	}

	/**
	 * 
	 */
	public RemoveElement() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RemoveElement re = new RemoveElement();
		int[] A = new int[] { 1, 2, 3, 4 };
		int elem = 4;
		System.out.printf("%d\n", re.removeElement(A, elem));
		for (int i = 0; i < A.length; i++) {
			System.out.printf("%d, ", A[i]);
		}
	}

}
